package com.fzebu.fastboot.uuid;

import java.util.Random;
import java.util.UUID;

/**
 * @author Jack
 * @version 1.0.0
 * @create 2020/2/23 17:02
 */
public class UuidUtil {

    /**
     * 0-9 A-Z a-z
     */
    public static final char[] CHARS = {'a', 'b', 'c', 'd', 'e', 'f', 'g',
            'h', 'i', 'j', 'k', 'l', 'm', 'n',
            'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
            'A', 'B', 'C', 'D', 'E', 'F', 'G',
            'H', 'I', 'J', 'K', 'L', 'M', 'N',
            'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

    public static final int CHARS_LENGTH = CHARS.length;

    /**
     * 获取30位数的uuid
     */
    public static String getUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    /**
     * 获取由雪花算法计算出的 UUID
     */
    public static long getNextId() {
        return SnowFlake.INSTANCE.getNextId();
    }

    /**
     * 获取指定位数的随机数
     * @param len 随机数长度
     */
    public static String getRandomUUID(int len) {
        if (len <= 0) {
            return null;
        }
        Random random = new Random();
        char[] chars = new char[len];
        if (len == 1) {
            chars[0] = CHARS[random.nextInt(CHARS_LENGTH)];
            return new String(chars);
        }
        for (int i = 0, size = len >> 1; i < size; i++) {
            chars[i] = CHARS[random.nextInt(CHARS_LENGTH)];
            chars[len - i - 1] = CHARS[random.nextInt(CHARS_LENGTH)];
        }
        return new String(chars);
    }
}
